package util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * 自动生成编号
 * @author 赵红娇
 * util
 * @date 2020年9月3日下午9:12:15
 */
public class AutoNo {
	/**
	 * 自动生成订单号
	 * @return
	 */
	public static String autoOrderFoodNo() {
		//订单号的格式是年月日+订单量
		String data=CastUtil.format(new Date(), CastUtil.YEAR_PATTERN);
		String dir=System.getProperty("user.dir");
		/**
		 * 先读取文件中的数字,然后累加,再写入文件
		 */
		long no=0;
		try{
			DataInputStream dis=new DataInputStream(new FileInputStream(new File(dir+"/data/OrderFoodNo.txt")));	
			no =dis.readLong();
			DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(dir+"/data/OrderFoodNo.txt")));
			//用数据流实现no的累加操作,一直累加
			dos.writeLong(++no);
		}catch (IOException e) {
			e.printStackTrace();
		}
		return data+no;
	}
	
	public static Long getVipNo() {
		DataInputStream dis = null;
		DataOutputStream oos = null;
		Date date = new Date();
		//202008121
		long no = 100;
		try {
			String dir = System.getProperty("user.dir");
			if(new File(dir+"/data/vip.bat").exists()) {
				dis = new DataInputStream(new FileInputStream(dir+"/data/vip.bat"));
				no = dis.readLong();
			}
			oos = new DataOutputStream(new FileOutputStream(dir+"/data/vip.bat"));
			oos.writeLong(++no);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				oos.close();
				if(dis!=null) {
					dis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return Long.valueOf(no);
	}
}

